/**
 * cateGory: 柱状图 + 折线图
 */
import { xAxisCommon, yAxisCommon, colorCommon } from '../common-option'
import { mergeOption, setSeries, setBarLineLegendColor } from '../util'
export default {
  name: 'barLine',
  option(param) {
    const seriesObj = getSeriesObj(param)
    const opts = mergeOption(param, {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          // 坐标轴指示器，坐标轴触发有效
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        }
      },
      legend: {
        bottom: 0,
        itemWidth: 18,
        itemHeight: 10,
        textStyle: {
          color: '#999'
        },
        data: []
      },
      color: colorCommon,
      grid: {
        left: '0',
        right: '0',
        top: '30px',
        bottom: '30px',
        containLabel: true
      },
      xAxis: {
        type: 'category',
        data: [],
        ...xAxisCommon
      },
      yAxis: [
        {
          type: 'value',
          name: '',
          min: 0,
          max: seriesObj.barMax,
          splitNumber: seriesObj.splitNumber,
          interval: seriesObj.barMax / seriesObj.splitNumber,
          ...yAxisCommon
        },
        {
          type: 'value',
          name: '',
          min: 0,
          max: seriesObj.lineMax,
          splitNumber: seriesObj.splitNumber,
          interval: seriesObj.lineMax / seriesObj.splitNumber,
          ...yAxisCommon
        }
      ],
      series: setSeries({ series: param.series }, { barLabelShow: false, lineLabelShow: false })
    })
    const rate = 8 / 18
    let legendArr = []
    if (Array.isArray(opts.legend)) {
      legendArr = opts.legend[0].data
    } else {
      legendArr = opts.legend.data
    }
    legendArr.forEach((item, i) => {
      opts.series.forEach((serie) => {
        if (serie.name === item) {
          const legend = {
            name: item
          }
          if (serie.type === 'bar') {
            legend.icon = 'rect'
            legend.itemStyle = setBarLineLegendColor(rate, opts.color[i])
          }
          legendArr[i] = legend
        }
      })
    })
    return opts
  }
}

function getSeriesObj(param) {
  const seriesObj = {
    bar: [],
    line: [],
    barMax: 0,
    lineMax: 0,
    splitNumber: 5
  }
  const barStack = {}
  param.series.forEach((item) => {
    if (item.type === 'bar') {
      if (item.stack) {
        if (!barStack[item.stack]) {
          barStack[item.stack] = item.data
        } else {
          barStack[item.stack] = barStack[item.stack].map((data, i) => {
            return data + item.data[i]
          })
        }
      } else {
        seriesObj.bar.push(item.data)
      }
    } else if (item.type === 'line' && !item.markLine) {
      seriesObj.line.push(item.data)
    }
  })
  for (const i in barStack) {
    seriesObj.bar.push(barStack[i])
  }
  seriesObj.bar = seriesObj.bar.flat()
  seriesObj.line = seriesObj.line.flat()
  seriesObj.barMax = calMax(seriesObj.bar)
  seriesObj.lineMax = calMax(seriesObj.line)
  seriesObj.splitNumber = calSplitNumber(seriesObj.barMax, seriesObj.lineMax, seriesObj.splitNumber)
  return seriesObj
}
// 计算最大值
function calMax(arr) {
  const max = Math.max(...arr)
  const maxint = Math.ceil(max / 10) // 不让最高的值超过最上面的刻度
  let maxval = maxint * 10 // 让显示的刻度是整数

  // 为了防止数据为0时，Y轴不显示，给个最大值
  if (maxval === 0) {
    maxval = 1
  }
  return maxval
}
// 计算splitNumber能让bar和line都能均匀分布(不出现小数)的最小整数，初始splitNumber = 5
function calSplitNumber(barMax, lineMax, splitNumber) {
  while (
    String(barMax / splitNumber).indexOf('.') > -1 ||
    String(lineMax / splitNumber).indexOf('.') > -1
  ) {
    splitNumber++
  }
  return splitNumber
}
